﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MODULE_KETOAN
{
    public partial class frm_banggiathumua : Form
    {
        ketoanDataContext context =cls_ketoan.getcontext();
        public frm_banggiathumua()
        {
            InitializeComponent();
        }
        private void load_data()
        {
            IEnumerable<dongiahanghoamoinhat> dongias = context.dongiahanghoamoinhats.AsEnumerable();
            if (dongias.Count() > 0)
            {
                grdata.Rows.Clear();
                for (int i = 0; i < dongias.Count(); i++)
                {
                    dongiahanghoamoinhat dongia = dongias.ElementAt(i);
                    grdata.Rows.Add();
                    grdata.Rows[i].Cells["datamahang"].Value = dongia.mahang;
                    grdata.Rows[i].Cells["datatenhang"].Value = dongia.tenhang;
                    grdata.Rows[i].Cells["datagianhap"].Value = dongia.dongia;

                }
            }
        }
        private void frm_banggiathumua_Load(object sender, EventArgs e)
        {
            try
            {
            


                load_data();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

      

     
        private void grsearch_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            //if (grsearch.Rows.Count > 0)
            //{
            //    if (e.ColumnIndex == 0)
            //    {
            //      
            //    }
            //    if (e.ColumnIndex == 0)
            //    {

            //    }
            //    if (e.ColumnIndex == 0)
            //    {

            //    }
            //}
        }

        private void txtmahang_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string keyword;

                keyword = txtmahang.Text; ;


                IEnumerable<dongiahanghoamoinhat> dongias = from c in context.dongiahanghoamoinhats where c.mahang.Contains(keyword) select c;
                grdata.Rows.Clear();
                for (int i = 0; i < dongias.Count(); i++)
                {
                    dongiahanghoamoinhat dongia = dongias.ElementAt(i);
                    grdata.Rows.Add();
                    grdata.Rows[i].Cells["datamahang"].Value = dongia.mahang;
                    grdata.Rows[i].Cells["datatenhang"].Value = dongia.tenhang;
                    grdata.Rows[i].Cells["datagianhap"].Value = dongia.dongia;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void txttenhang_TextChanged(object sender, EventArgs e)
        {
            string tenhang;

            tenhang = this.txttenhang.Text; ;


            IEnumerable<dongiahanghoamoinhat> dongias = from c in context.dongiahanghoamoinhats where c.tenhang.Contains(tenhang) select c;
            grdata.Rows.Clear();
            for (int i = 0; i < dongias.Count(); i++)
            {
                dongiahanghoamoinhat dongia = dongias.ElementAt(i);
                grdata.Rows.Add();
                grdata.Rows[i].Cells["datamahang"].Value = dongia.mahang;
                grdata.Rows[i].Cells["datatenhang"].Value = dongia.tenhang;
                grdata.Rows[i].Cells["datagianhap"].Value = dongia.dongia;

            }
        }

        private void grdata_Click(object sender, EventArgs e)
        {
            if (grdata.Rows.Count > 0)
            {
                if (grdata.CurrentRow.Cells["datamahang"].Value != null)
                {
                    this.txtmahang.Text = grdata.CurrentRow.Cells["datamahang"].Value.ToString();
                }
                else
                { this.txtmahang.Text = "";
                }
                if (grdata.CurrentRow.Cells["datatenhang"].Value != null)
                {
                    this.txttenhang.Text = grdata.CurrentRow.Cells["datatenhang"].Value.ToString();
                }
                else
                {
                    this.txttenhang.Text = "";
                }
                if (grdata.CurrentRow.Cells["datagianhap"].Value != null)
                {
                    this.txtgianhap.Text = grdata.CurrentRow.Cells["datagianhap"].Value.ToString();
                }
                else
                {
                    this.txtgianhap.Text = "";
                }
            }
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (this.txtmahang.Text != "")
            { 
                string mahang = this.txtmahang.Text;
                IEnumerable<DM_hanghoa> hanghoas = from c in context.DM_hanghoas where c.mahang.Equals(mahang) select c;
                if (hanghoas.Count() > 0)
                {
                    DM_dongiahanghoa dongiahanghoa = new DM_dongiahanghoa();
                    dongiahanghoa.mahang = mahang;
                    DateTime d = context.p_getdate().ElementAt(0).serverdatetime;
                    dongiahanghoa.ngay = d;
                    double gia;
                    try
                    {
                        gia = double.Parse(txtgianhap.Text);
                        dongiahanghoa.dongia=gia;
                        context.DM_dongiahanghoas.InsertOnSubmit(dongiahanghoa);
                        context.SubmitChanges();
                        MessageBox.Show("Cập nhât thành công.");
                        load_data();


                    }catch(Exception ex)
                    {
                        MessageBox.Show("Nhập giá không đúng.Phải nhâp số");
                    }
                      
                    
                    
                }
                else
                {
                    MessageBox.Show("Không có mặt hàng này trong danh mục.");
                }
            }
            else
            {
                MessageBox.Show("Mã hàng rỗng");
            }
           

        }

        private void txtmahang_Validated(object sender, EventArgs e)
        {
           
        }

        private void txtmahang_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    string mahanghoa = this.txtmahang.Text;

                    if (mahanghoa != "")
                    {
                        IEnumerable<DM_hanghoa> hanghoas = from c in context.DM_hanghoas where c.mahang.Equals(mahanghoa) select c;
                        if (hanghoas.Count() > 0)
                        {
                            DM_hanghoa hanghoa = hanghoas.ElementAt(0);
                            this.txttenhang.Text = hanghoa.tenhang;
                            IEnumerable<dongiahanghoamoinhat> dongias = from c in context.dongiahanghoamoinhats where c.mahang.Equals(mahanghoa) select c;
                            if (dongias.Count() > 0)
                            {
                                this.txtgianhap.Text = dongias.ElementAt(0).dongia.ToString();
                            }


                        }
                        else
                        {

                            //Tim kiem hanghoa
                            MODULE_TIENICH.cls_tienich cls = new MODULE_TIENICH.cls_tienich();
                            cls.ShowForm("frm_timkiemhanghoa");
                            this.txtmahang.Text = cls._mahanghoa;
                            hanghoas = from c in context.DM_hanghoas where c.mahang.Equals(cls._mahanghoa) select c;
                           DM_hanghoa hanghoa = hanghoas.ElementAt(0);
                            this.txttenhang.Text =hanghoa.tenhang;
                            IEnumerable<dongiahanghoamoinhat> dongias = from c in context.dongiahanghoamoinhats where c.mahang.Equals(cls._mahanghoa) select c;
                            if (dongias.Count() > 0)
                            {
                                this.txtgianhap.Text = dongias.ElementAt(0).dongia.ToString();
                            }
                        }
                    }
                    else
                    {
                        //Tim kiem hanghoa
                            MODULE_TIENICH.cls_tienich cls = new MODULE_TIENICH.cls_tienich();
                            cls.ShowForm("frm_timkiemhanghoa");
                            this.txtmahang.Text = cls._mahanghoa;
                            IEnumerable<DM_hanghoa>  hanghoas = from c in context.DM_hanghoas where c.mahang.Equals(cls._mahanghoa) select c;
                           DM_hanghoa hanghoa = hanghoas.ElementAt(0);
                            this.txttenhang.Text =hanghoa.tenhang;
                            IEnumerable<dongiahanghoamoinhat> dongias = from c in context.dongiahanghoamoinhats where c.mahang.Equals(cls._mahanghoa) select c;
                            if (dongias.Count() > 0)
                            {
                                this.txtgianhap.Text = dongias.ElementAt(0).dongia.ToString();
                            }
                        }
                    }
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      
       
      
       
      

      
       

        
    }
}
